<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>parallax</title>
</head>
<style>
    * {
        width: 100%;
        height: 100%;
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }

    body {
        overflow: hidden;
    }

    #container {
        height: 100%;
        position: relative;
    }

    .item {
        width: 100%;
        height: 100%;
        position: absolute;
        transition: all 1s linear;
    }

    .item img {
        width: 100%;
        height: 100%;
        position: absolute;
        /* object-fit: cover; */
    }

    .pre,
    .next {
        height: 0;
        z-index: 1;
    }

    .next {
        bottom: 0;
    }

    .next img {
        bottom: 0;
        /* transform: translateY(10%); */
    }

    .scroll-down .pre {
        height: 100%;
    }

    .scroll-up .next {
        height: 100%;
    }

    .scroll-down .item .cur img {
        /* transform: translateY(-10%); */
    }
</style>

<body>
    <div id="container"></div>
</body>
<script>

    const path = [
        '../public/img/1.jpg',
        '../public/img/2.jpg',
        '../public/img/3.jpg',
        '../public/img/4.jpg',
        '../public/img/5.jpg',
        '../public/img/6.jpg'
    ]


    const container = document.getElementById('container')
    let curIndex = 0

    function resetElement() {
        container.innerHTML = ''
        const preIndex = curIndex - 1 < 0 ? path.length - 1 : curIndex - 1
        const nextIndex = curIndex + 1 > path.length - 1 ? 0 : curIndex + 1

        console.log(preIndex, curIndex, nextIndex)
        createItem(preIndex).classList.add('pre')
        createItem(curIndex).classList.add('cur')
        createItem(nextIndex).classList.add('next')
    }

    function createItem(index) {
        const imgUrl = path[index]
        const item = document.createElement('div')
        item.classList.add('item')
        item.innerHTML = `<img src='${imgUrl}'/>`
        container.appendChild(item)
        return item
    }

    resetElement()

    let isScroll = false
    container.addEventListener('wheel', e => {
        if (isScroll) {
            return;
        }

        isScroll = true

        if (e.deltaY > 0) {
            container.classList.add('scroll-down')
            curIndex = curIndex - 1 < 0 ? path.length - 1 : curIndex - 1
        } else {
            container.classList.add('scroll-up')
            curIndex = curIndex + 1 > path.length - 1 ? 0 : curIndex + 1
        }
    })

    container.addEventListener('transitionend', () => {
        isScroll = false
        container.classList.remove('scroll-down')
        container.classList.remove('scroll-up')
        resetElement()
    })
</script>

</html>